import { _decorator, Component, Label, Node, ProgressBar, resources, Sprite, SpriteFrame } from 'cc';
import { UILayerMgr } from './UILayerMgr';
import { UserInfo } from './UserInfo';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('PetItem')
export class PetItem extends Component {
    @property(Label)
    chipLabel:Label;
    @property(Label)
    nameLabel:Label;
    @property(Node)
    IsGoBattleIconNode:Node;
    @property(ProgressBar)
    chipProgressBar:ProgressBar
    @property(Node)
    isNotHasNode:Node;
    @property(Sprite)
    sp:Sprite;
    @property(Sprite)
    cellBg:Sprite;
    @property(Label)
    levelLabel:Label
    @property(Node)
    gangtanhaoIconNode:Node;
    @property(Node)
    chipIconNode:Node;

    pet;
    _isClickTopContent;

    init(data, isClickTopContent) {
        this._isClickTopContent=isClickTopContent
        this.pet = data;
        console.log(data,"data")
        this.nameLabel.string = data.petTp.name;
            this.levelLabel.string = data.petLevel;
   
            this.chipLabel.string = `${UserInfo.hasPetChipArr[data.petTp.petType]}/${data.petTp.totalChipNum}`
            this.chipProgressBar.progress = UserInfo.hasPetChipArr[data.petTp.petType] / data.petTp.totalChipNum;
      
        if (isClickTopContent) {
            this.isNotHasNode.active = false;
            this.chipProgressBar.node.active = false;
            this.gangtanhaoIconNode.active = false;
            this.chipIconNode.active = false;
        }
        else{
            this.chipIconNode.active=true;
            this.isNotHasNode.active=true;
            this.chipProgressBar.node.active=true;

            if (UserInfo.hasPetChipArr[data.petTp.petType]>=data.petTp.totalChipNum) {
                this.gangtanhaoIconNode.active = true;
            }
            else{
                this.gangtanhaoIconNode.active = false;
            }
        }
        if (data.isGoBattle) {
            this.IsGoBattleIconNode.active=true;
        }
        else{
            this.IsGoBattleIconNode.active=false;
        }

        resources.load(`${data.petTp.imgPath}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.sp.spriteFrame=res;
        })
        resources.load(`qualityCellBg/${data.petTp.quality}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.cellBg.spriteFrame=res;
        })

    }

    onclickThisCom() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        UILayerMgr.getIns.openUILayer("desPetPanel",{pet:this.pet,isClickTopContent:this._isClickTopContent});
    }
}


